<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="岗位编码" prop="postCode">
           <el-input
              v-model="queryParams.postCode"
              placeholder="请输入岗位编码"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item label="岗位名称" prop="postName">
           <el-input
              v-model="queryParams.postName"
              placeholder="请输入岗位名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item label="状态" prop="status">
           <el-select v-model="queryParams.status" placeholder="岗位状态" clearable style="width: 200px">
              <el-option
                 v-for="dict in sys_normal_disable"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item>
        <el-form-item>
           <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
           <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
     </el-form>

     <el-row :gutter="10" class="mb8">
        <el-col :span="1.5"><el-button type="primary" plain icon="Plus" @click="handleAdd"  >新增</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:post:edit']"
           >修改</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:post:remove']"
           >删除</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['system:post:export']"
           >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
     </el-row>

     <el-table v-loading="loading" :data="postList">
      <el-table-column align="center" label="申请编号" prop="id" >
         <template #default="scope">
               <span>{{ scope.row.id.toString()}}</span>
            </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="checkstatus">
        <template #default="scope">
          <dict-tag :options="bpm_process_instance_status" :value="scope.row.checkstatus" />
        </template>
      </el-table-column>
      <el-table-column
       
        align="center"
        label="开始时间"
        prop="startTime"
        width="180"
        >
      <template #default="scope">
               <span>{{ parseTime(scope.row.startTime) }}</span>
            </template>
      </el-table-column>
      <el-table-column
       
        align="center"
        label="结束时间"
        prop="endTime"
        width="180"
        >
      <template #default="scope">
               <span>{{ parseTime(scope.row.endTime) }}</span>
            </template>
      </el-table-column>
      <el-table-column align="center" label="请假类型" prop="type">
        <!-- <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_OA_LEAVE_TYPE" :value="scope.row.type" />
        </template> -->
      </el-table-column>
      <el-table-column align="center" label="原因" prop="reason" />
      <el-table-column
        align="center"
        label="申请时间"
        prop="createTime"
        width="180"
      >
      <template #default="scope">
               <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template #default="scope">
          
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-button>
          <el-button    
             v-if="scope.row.checkstatus !=-1"     
            link
            type="primary"
            @click="handleProcessDetail(scope.row)"
          >
            进度
          </el-button>
          <el-button
            v-if="scope.row.checkstatus === 1"
            link
            type="danger"
            @click="cancelLeave(scope.row)"
          >
            撤消
          </el-button>
          <el-button
            v-if="scope.row.checkstatus ==-1"
            link
            type="warning"
            @click="apply(scope.row)"
          >
            重新申请
          </el-button>
            <!-- <el-button
            v-if="scope.row.status === -1"
            link
            type="primary"
            @click="cancelLeave(scope.row)"
          >
            发起申请
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

     <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
     />

     <!-- 添加或修改岗位对话框 -->
     <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
          ref="formRef"
          v-loading="formLoading"
          :model="form"
          :rules="rules"
          label-width="80px"
  >
    <el-form-item label="请假类型" prop="type">
      <el-select v-model="form.type" clearable placeholder="请选择请假类型">
        <el-option value="1">病假</el-option>
        <el-option value="2">事假</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="form.startTime"
        clearable
        placeholder="请选择开始时间"
        type="datetime"
       
        value-format="x"
      />
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
        v-model="form.endTime"
        clearable
        placeholder="请选择结束时间"
       
        type="datetime"
        value-format="x"
      />
    </el-form-item>
    <el-form-item label="原因" prop="reason">
      <el-input v-model="form.reason" placeholder="请输请假原因" type="textarea" />
    </el-form-item>
        </el-form>
        <template #footer>
           <div class="dialog-footer">
              <el-button type="primary" @click="submitForm(0)">暂存</el-button>
              <el-button type="primary" @click="submitForm(1)">提交</el-button>
              <el-button @click="cancel">取 消</el-button>
           </div>
        </template>
     </el-dialog>

      <CheckModal v-if="checkModelVisible" v-model="checkModelVisible" :row="rowdata" />


  </div>
</template>

<script setup name="leave">


import * as LeaveApi from '@/api/flowable/demo/leave'
import CheckModal from "./CheckModal.vue";
import{cancelProcessInstanceByStartUser}from "@/api/flowable/instance";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const { bpm_process_instance_status } = proxy.useDict("bpm_process_instance_status");
const router = useRouter();
const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const checkModelVisible = ref(false);
const rowdata=ref([]);
const userStore = useUserStore();

const data = reactive({
 form: {},
 queryParams: {
   pageNum: 1,
   pageSize: 10,
   type: undefined,
  status: undefined,
  reason: undefined,
  createTime: []
 },
 rules: {
  type: [{ required: true, message: '请假类型不能为空', trigger: 'blur' }],
  reason: [{ required: true, message: '请假原因不能为空', trigger: 'change' }],
  startTime: [{ required: true, message: '请假开始时间不能为空', trigger: 'change' }],
  endTime: [{ required: true, message: '请假结束时间不能为空', trigger: 'change' }]
 }
});


const { queryParams, form, rules } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  console.log("444",userStore.id)
 loading.value = true;
 LeaveApi.getLeavePage(queryParams.value).then(response => {
   postList.value = response.rows;
   total.value = response.total;
   loading.value = false;
 });
}
    /** 流程流转记录 */
  function  handleProcessDetail(row){
      router.push({ name: 'BpmProcessInstanceDetail',
        query: {
          procInsId: row.processInstanceId,
      }})
    }

    function handleDetail(row){
      console.log("66",row)
       rowdata.value=row
           checkModelVisible.value = true;
          
    }
    function apply(row){
        proxy.$modal.loading("请稍候...");
        LeaveApi.apply(userStore.id,row.id,'bpm_oa_leave','flow_c44xhr07').then(response => {
            proxy.$modal.closeLoading();
           queryParams.value.pageNum = 1;
         getList();
 });
    }
/** 取消按钮 */
function cancel() {
 open.value = false;
 reset();
}



function cancelLeave (rowdata)  {
  
  proxy.$modal.confirm('撤销后，该审批流程将自动结束，是否确认取消？').then(function () {
  proxy.$modal.loading("请稍候...");
    return   cancelProcessInstanceByStartUser(
      {
        id: rowdata.processInstanceId,
        reason: ""
     }
    )
  }).then((res) => {
      proxy.$modal.closeLoading();
    console.log("resres",res)
    getList();
     proxy.$modal.msgSuccess('操作成功')
  
   })
  
  
}

/** 表单重置 */
function reset() {
  form.value = {
  id:0,
  type: undefined,
  reason: undefined,
  startTime: undefined,
  endTime: undefined,
  submittype:undefined
 };
 proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
 queryParams.value.pageNum = 1;
 getList();
}

/** 重置按钮操作 */
function resetQuery() {
 proxy.resetForm("queryRef");
 handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
 ids.value = selection.map(item => item.id);
 single.value = selection.length != 1;
 multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
 reset();
 open.value = true;
 title.value = "请假";
}

/** 修改按钮操作 */
function handleUpdate(row) {
 reset();
 const postId = row.id || ids.value;
 getPost(id).then(response => {
   form.value = response.data;
   open.value = true;
   title.value = "请假";
 });
}

/** 提交按钮 */
function submitForm(submittype) {
  form.value.submittype=submittype
 proxy.$refs["formRef"].validate(valid => {
   if (valid) {
     if (form.value.id != 0) {
       updatePost(form.value).then(response => {
         proxy.$modal.msgSuccess("修改成功");
         open.value = false;
         getList();
       });
     } else {
      LeaveApi.createLeave(form.value).then(response => {
         proxy.$modal.msgSuccess("新增成功");
         open.value = false;
         getList();
       });
     }
   }
 });
}

/** 删除按钮操作 */
function handleDelete(row) {
 const postIds = row.postId || ids.value;
 proxy.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
   return delPost(postIds);
 }).then(() => {
   getList();
   proxy.$modal.msgSuccess("删除成功");
 }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
 proxy.download("system/post/export", {
   ...queryParams.value
 }, `post_${new Date().getTime()}.xlsx`);
}

getList();
</script>
